package com.bc.ui;

import java.util.ArrayList;
import java.util.HashMap;

import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;
import kankan.wheel.widget.adapters.WheelViewAdapter;

import com.bc.R;
import com.bc.db.dao.WarnDao;
import com.bc.db.entity.Warn;
import com.bc.utils.Constants;
import com.umeng.analytics.MobclickAgent;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class WarnAddActivity extends BaseActivity {
	
	private WheelView mTypeWheelView, mPriceWheelView;
	private static ArrayList<HashMap<String, Integer>> wheelList = new ArrayList<HashMap<String,Integer>>();
	private static HashMap<String, String[]> dataHashMap = new HashMap<String, String[]>();
	static {
		for (int i = 0; i< 2; i++) {
			HashMap<String, Integer> mHashMap = new HashMap<String, Integer>();
			mHashMap.put("type", i);
			mHashMap.put("price", i);
			wheelList.add(mHashMap);
		}
		
	}
	
	private TextView etPrice;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_add_warn);
		
		mTypeWheelView = (WheelView)findViewById(R.id.warn_option_wheel);
		mPriceWheelView = (WheelView)findViewById(R.id.warn_price_wheel);
		
		dataHashMap.put("type", getResources().getStringArray(R.array.warn_type_array));
		dataHashMap.put("price", getResources().getStringArray(R.array.warn_price_array));
		
		mTypeWheelView.setViewAdapter(createWheelViewAdapter("type"));
		mPriceWheelView.setViewAdapter(createWheelViewAdapter("price"));
		mTypeWheelView.setCurrentItem(0);
		mPriceWheelView.setCurrentItem(0);
		
		etPrice = (EditText)findViewById(R.id.etPrice);
		
		ActionBar actionBar = getSupportActionBar();
		actionBar.setTitle(R.string.warn_add_tip);
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setDisplayShowHomeEnabled(true);
	}
	
	public WheelViewAdapter createWheelViewAdapter(final String token) {
		return new ArrayWheelAdapter<String>(WarnAddActivity.this, new String[wheelList.size()]){

			@Override
			public CharSequence getItemText(int index) {
				return dataHashMap.get(token)[wheelList.get(index).get(token)];
			}
			
		};
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_ok:
			addWarn();
			break;
		case android.R.id.home:
			finish();
			overridePendingTransition(R.anim.push_in_left, R.anim.push_out_right);
			break;
		default:
			break;
		}
		return true;
	}
	
	public void addWarn() {
		String price = etPrice.getText().toString().trim();
		if (TextUtils.isEmpty(price)) {
			Toast.makeText(WarnAddActivity.this, getResources().getString(R.string.warn_error_tip), Toast.LENGTH_SHORT).show();
			return;
		}
		int price_type = mPriceWheelView.getCurrentItem();
		int warn_type = mTypeWheelView.getCurrentItem();
		Log.d(getClass().getSimpleName(), "price_type: " + price_type + "/warn_type: " + warn_type);
		WarnDao dao = WarnDao.getInstance(WarnAddActivity.this);
		Warn warn = new Warn();
		warn.money = Double.valueOf(price);
		warn.over = warn_type;
		warn.buy = price_type;
		warn.type = getIntent().getStringExtra(Constants.BTC_TYPE_COL);
		warn.enable = 0;
		dao.addWarn(warn);
		finish();
		overridePendingTransition(R.anim.push_in_left, R.anim.push_out_right);
		MobclickAgent.onEvent(WarnAddActivity.this, "addWarn");
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_setting, menu);
		return true;
	}
}
